//
//  WBEmotionTool.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/18.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit
import YYModel

class WBEmotionTool: NSObject {
    var dataSourceArr: [[[WBEmotionModel]]] {
        //1. 解析plist获取一维数组
        //最近的一维数组
        let recentEmotions = parserPlist(path: defaultPath)
        //最近的二维数组
        let devidedRecent = devideEmotions(emotions: recentEmotions)
        
        //默认
        let defaultEmotions = parserPlist(path: defaultPath)
        let devidedDefaluts = devideEmotions(emotions: defaultEmotions)
        
        //emoji
        let emojiEmtions = parserPlist(path: emojiPath)
        let devidedEmoji = devideEmotions(emotions: emojiEmtions)
        
        //lxh
        let lxhEmtions = parserPlist(path: lxhPath)
        let devidedLxh = devideEmotions(emotions: lxhEmtions)
        
        //返回一个三维数组
        return [devidedRecent, devidedDefaluts, devidedEmoji, devidedLxh]
    }
    
    /// 所有的表情的路径
    var resourcePath: String {
        let bundlePath = (Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil))!
        return bundlePath + "/Contents/Resources/"
    }
    
    /// 默认表情的路径
    var defaultPath: String {
        return resourcePath + "default/info.plist"
    }
    
    /// emoji表情的路径
    var emojiPath: String {
        return resourcePath + "emoji/info.plist"
    }
    
    /// 浪小花表情的路径
    var lxhPath: String {
        return resourcePath + "lxh/info.plist"
    }
    
    /// 解析plist文件，生成一个大的一维数组
    ///
    /// - Parameter path: plist的路径
    /// - Returns: 一维数组
    func parserPlist(path: String) -> [WBEmotionModel] {
        let arr = NSArray(contentsOfFile: path) as! [[String: Any]]
        let emotions = NSArray.yy_modelArray(with: WBEmotionModel.self, json: arr) as! [WBEmotionModel]
        for emotion in emotions {
            //图片表情才需要设全路径
            if emotion.type == 0 {
                let folderPath = (path as NSString).deletingLastPathComponent
                emotion.fullPath = folderPath + "/" + emotion.png!
            }
        }
        return emotions
    }
    
    /// 将大的一维数组按cell进行分组
    ///
    /// - Parameter emotions: 将要被分组的emotions
    /// - Returns: 二维数组
    func devideEmotions(emotions: [WBEmotionModel]) -> [[WBEmotionModel]] {
        //分页算法，一共多个条数据，每页显示几个，一共多少页
        let pages = (emotions.count - 1)/20 + 1
        
        var location = 0
        var length = 20
        
        var devidedEmotions: [[WBEmotionModel]] = []
        
        for i in 0..<pages {
            location = 20 * i
            
            //如果最后一个cell不足20个，有几个显示几个
            if emotions.count - location < 20 {
                length = emotions.count - location
            }
            
            let range = NSMakeRange(20 * i, length)
            let subArr = (emotions as NSArray).subarray(with: range) as! [WBEmotionModel]
            
            devidedEmotions.append(subArr)
        }
        
        return devidedEmotions
    }
}
